import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
from sklearn.model_selection import train_test_split

##随机森林
df = pd.read_csv('D:/patientdata2.csv',encoding='gb18030')
df = df.loc[:, ~df.columns.str.contains('^Unnamed')]
print(df.head())
#  x为可能导致糖尿病的因素
x = df.drop('Level',axis=1)
#  y为肺癌严重程度
y = df['Level']

def random_forest():
    X=df.drop('Level',axis=1)
    #  一共1000个数据950个作为训练集，50个作为测试集
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=50 / 1000,random_state=0)
    # criterion = 'entropy'信息熵：在结果出来之前对可能产生的信息量的期望
    classifier = RandomForestClassifier(criterion='entropy',n_estimators=1000,max_depth=None,min_samples_split=0.1,
                                        min_weight_fraction_leaf=0.02)
    classifier.fit(X_train, y_train)
    y_pred = classifier.predict(X_test)
    print("随机森林准确率：")
    print(confusion_matrix(y_test, y_pred))
    print(classification_report(y_test, y_pred))
    print(accuracy_score(y_test, y_pred))
    print('模型得分: {:2f}'.format(classifier.score(X_test, y_test)))
    y_ = np.array(y_test)
    print("随机森林预测结果:", classifier.predict(X_test))
    print('--------------------------------------------------------------------------')
    print('真实结果:         ', y_)

if __name__ == '__main__':
    random_forest()

#训练数据（KNN）寻找最佳的k值
def k_nn():
    X = df.drop('Level', axis=1)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=50 / 1000, random_state=0)
    # scaler = StandardScaler()
    # scaler.fit(X_train)
    # x_train = scaler.transform(X_train)
    # x_test = scaler.transform(X_test)
    error = []
    # 由于一开始并不清楚K取多少准确率最高，所以写了一个K为1-14的for循环，通过检查误差值来判断最合适的K值
    for k in range(99, 101):
        classifier = KNeighborsClassifier(n_neighbors=k)
        classifier.fit(X_train, y_train)
        y_prediction = classifier.predict(X_test)
        error.append(np.mean(y_prediction != y_test))
        print('当k=', k, '时的准确率')
        print(confusion_matrix(y_test, y_prediction))
        print(classification_report(y_test, y_prediction))
        print('模型得分:{:.2f}'.format(classifier.score(X_test, y_test)))


if __name__ == '__main__':
    k_nn()
